from pyecharts import options as opts
from pyecharts.charts import Bar
import pandas as pd

df = pd.read_excel('new1.xlsx', index_col=0)
# 获取x轴数据
x_data = list(df.index)

# 获取分类和图列数据
legend_data = list(df.columns)

# 获取y轴数据
y_data = []
for col in df.columns:
    y_data.append(list(df[col]))

# 创建Bar对象
bar = Bar()

# 添加x轴数据
bar.add_xaxis(x_data)

# 添加y轴数据和图列
for i in range(len(legend_data)):
    bar.add_yaxis(
        legend_data[i],
        y_data[i],
        label_opts=opts.LabelOpts(is_show=False)
    )

# 设置全局配置项
bar.set_global_opts(
    title_opts=opts.TitleOpts(title="二十大和政府报告主题强度分组柱状图"),
    legend_opts=opts.LegendOpts(pos_right=True),
    xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-90)),
    yaxis_opts=opts.AxisOpts(name="强度")
)

# 渲染图表
bar.render("grouped_bar_chart.html")